package com.huoyun.order.service;

import java.util.Date;
import java.util.List;
import java.text.SimpleDateFormat;

import com.huoyun.base.bean.Page;
import com.huoyun.car.dao.CarPubInfoDao;
import com.huoyun.car.model.BidCarInfoModel;
import com.huoyun.cargo.bean.CargoInfo;
import com.huoyun.cargo.dao.CargoInfoDao;
import com.huoyun.common.dao.BidInfoDao;
import com.huoyun.order.bean.Order;
import com.huoyun.order.bean.OrderLog;
import com.huoyun.order.dao.OrderDao;
import com.huoyun.order.dao.OrderLogDao;
import com.huoyun.order.service.evt.GetOrderForIndexEvt;
import com.huoyun.order.service.evt.QueryOrderListEvt;
import com.huoyun.order.service.evt.SaveOrderEvt;
import com.huoyun.order.service.evt.UpdateOrderStatusEvt;
import com.huoyun.user.bean.User;

public class OrderServiceImpl implements OrderService {

	private OrderDao orderDao;
	
	private OrderLogDao orderLogDao;
	
	private CargoInfoDao cargoInfoDao;
	
	private CarPubInfoDao carPubInfoDao;
	
	private BidInfoDao bidInfoDao;
	
	@Override
	public void saveOrder(SaveOrderEvt saveOrderEvt) 
	{
		Order order = new Order();
		order.setAddressId(saveOrderEvt.getAddressId());
		order.setCargisTitle(saveOrderEvt.getCargisTitle());
		order.setCargosId(saveOrderEvt.getCargosId());
		order.setCargosOwnerId(saveOrderEvt.getCargosOwnerId());
		order.setCarId(saveOrderEvt.getCarId());
		order.setCarNumber(saveOrderEvt.getCarNumber());
		order.setCarOwnerId(saveOrderEvt.getCarOwnerId());
		order.setDiscount(saveOrderEvt.getDiscount());
		order.setOrderId(getOrderId());
		order.setPay(false);
		order.setPrice(saveOrderEvt.getPrice());
		order.setStatus(1);
		//保存订单
		long orderId = orderDao.saveOrder(order);
		
		OrderLog orderLog = new OrderLog();
		orderLog.setOrderId(orderId);
		orderLog.setOperaterId(saveOrderEvt.getOperateId());
		orderLog.setOperaterName(saveOrderEvt.getOperateName());
		//创建订单
		orderLog.setLogType(1);
		orderLog.setLogInfo("创建订单");
	    //保存订单的创建日志
		orderLogDao.saveOrderLog(orderLog);
		
	}

	@Override
	public void update(Order order) 
	{
		orderDao.update(order);
	}
	
	@Override
	public void updateOrderStatus(UpdateOrderStatusEvt updateOrderStatusEvt)
	{
		orderDao.updateOrderStatus(updateOrderStatusEvt.getOrderId(), updateOrderStatusEvt.getStatus());
		
		OrderLog orderLog = new OrderLog();
		orderLog.setOrderId(updateOrderStatusEvt.getOrderId());
		orderLog.setOperaterId(updateOrderStatusEvt.getOperateId());
		orderLog.setOperaterName(updateOrderStatusEvt.getOperateName());
		
		if(updateOrderStatusEvt.getStatus() == 1)
		{
			//创建订单
			orderLog.setLogType(1);
			orderLog.setLogInfo("创建订单");
		}
		else if(updateOrderStatusEvt.getStatus() == 2)
		{
			//付保证金
			orderLog.setLogType(2);
			orderLog.setLogInfo("货主支付保证金");
		}
		else if(updateOrderStatusEvt.getStatus() == 3)
		{
			//装完货物
			orderLog.setLogType(3);
			orderLog.setLogInfo("完成装货");
		}
		else if(updateOrderStatusEvt.getStatus() == 4)
		{
			//完成费用支付
			orderLog.setLogType(4);
			orderLog.setLogInfo("货主完成费用支付");
		}
		else if(updateOrderStatusEvt.getStatus() == 5)
		{
			//完成卸货
			orderLog.setLogType(5);
			orderLog.setLogInfo("完成卸货");
		}
		else if(updateOrderStatusEvt.getStatus() == 99)
		{
			//异常终止
			orderLog.setLogType(99);
			orderLog.setLogInfo("异常终止");
		}
		
	    //保存订单的操作日志
		orderLogDao.saveOrderLog(orderLog);
	}

	@Override
	public Order getOrderById(long orderId) 
	{
		return orderDao.getOrderById(orderId);
	} 

	@Override
	public void delete(long orderId) 
	{
		orderDao.delete(orderId);
	}
	
	/**
	 * 生成订单
	 * @param goodsId 订单关联的货物id
	 * @param carIds 订单关联的车辆id集合
	 * @param userInfo 当前操作的用户对象
	 * @param payType 支付方式
	 */
	@Override
	public void addOrder(long goodsId, String[] carIds, User userInfo, String payType)
	{
		//查询所有应标车辆信息集合
		List<BidCarInfoModel> bidCarList = carPubInfoDao.queryBidCars(goodsId, carIds);
		
		//货物对象
		CargoInfo cargoInfo = cargoInfoDao.getCargoInfo(userInfo.getId(), goodsId);
		
		//如果一个货物选择了多辆车辆，需要生成多个订单
		if(bidCarList != null && bidCarList.size() > 0)
		{
			for(BidCarInfoModel bidCarInfo : bidCarList)
			{
				SaveOrderEvt saveOrderEvt = new SaveOrderEvt();
				saveOrderEvt.setAddressId(1);
				saveOrderEvt.setCargisTitle(cargoInfo.getTitle());
				saveOrderEvt.setCargosId(cargoInfo.getId());
				saveOrderEvt.setCargosOwnerId(cargoInfo.getUserId());
				saveOrderEvt.setCarId(bidCarInfo.getCarId());
				saveOrderEvt.setCarNumber(bidCarInfo.getCarNumber());
				saveOrderEvt.setCarOwnerId(bidCarInfo.getUserId());
				saveOrderEvt.setDiscount("0");//无折扣
				saveOrderEvt.setPayType(payType);
				saveOrderEvt.setPrice(bidCarInfo.getBidInfoPrice());
				saveOrderEvt.setOperateId(userInfo.getId());
				saveOrderEvt.setOperateName(userInfo.getUsername());
				
				this.saveOrder(saveOrderEvt);
				
				//将相应的车源信息下线，防止与其他货源产生竞标关系
				carPubInfoDao.updateStatus(bidCarInfo.getId(), 0);
				
				//将相应的竞标信息状态修改为已成交
				bidInfoDao.updateStatus(Long.parseLong(bidCarInfo.getBidInfoId()), 1);
			}
		}
		
		//更新货物的状态为：生成订单
		cargoInfoDao.updateStatus(userInfo.getId(), goodsId, 3);
	}
	
	@Override
	public Page queryList(QueryOrderListEvt queryOrderListEvt) 
	{
		//构造dao需要的参数
		com.huoyun.order.dao.evt.QueryOrderListEvt evt = new com.huoyun.order.dao.evt.QueryOrderListEvt();
		evt.setAddTime(queryOrderListEvt.getAddTime());
		evt.setCargosOwnerId(queryOrderListEvt.getCargosOwnerId());
		evt.setCarOwnerId(queryOrderListEvt.getCarOwnerId());
		evt.setPageCount(queryOrderListEvt.getPageCount());
		evt.setPageNo(queryOrderListEvt.getPageNo());
		evt.setStatus(queryOrderListEvt.getStatus());
		
		//获取订单的总记录数
		long totleRecord = orderDao.queryOrderCount(evt);
		
		Page page = new Page();
		page.setPageNo(queryOrderListEvt.getPageNo());
		page.setPageCount(queryOrderListEvt.getPageCount());
		
		//设置总记录数
		page.setTotalRecord(totleRecord);
		
		//设置显示的记录列表
		page.setObjectList(orderDao.queryList(evt));
		
		return page;
	}
	
	/**
	 * 获取轻量型order，不包含User信息，OrderLightModel包含货物信息，订单信息，车辆信息
	 * @param queryOrderListEvt
	 * @return
	 * @author yang
	 */
	@Override
	public Page queryLightModels(QueryOrderListEvt queryOrderListEvt) {
		// TODO Auto-generated method stub
		com.huoyun.order.dao.evt.QueryOrderListEvt evt=new com.huoyun.order.dao.evt.QueryOrderListEvt();
		evt.setAddTime(queryOrderListEvt.getAddTime());
		evt.setCargosOwnerId(queryOrderListEvt.getCargosOwnerId());
		evt.setCarOwnerId(queryOrderListEvt.getCarOwnerId());
		evt.setStatus(queryOrderListEvt.getStatus());
		evt.setPageCount(queryOrderListEvt.getPageCount());
		evt.setPageNo(queryOrderListEvt.getPageNo());
		
		
		Page page=new Page();
		page.setTotalRecord(10);
		page.setPageCount(queryOrderListEvt.getPageCount());
		page.setPageNo(queryOrderListEvt.getPageNo());
		page.setObjectList(orderDao.queryLightModels(evt));
		return null;
	}
	
	/**
	 * 首页获取正在交易信息 
	 * @author yang
	 */
	@Override
	public Page getOrderForIndex(GetOrderForIndexEvt getOrderForIndexEvt) {
		com.huoyun.order.dao.evt.GetOrderForIndexEvt evt = new com.huoyun.order.dao.evt.GetOrderForIndexEvt();
		evt.setPageCount(getOrderForIndexEvt.getPageCount());
		evt.setPageNo(getOrderForIndexEvt.getPageNo());
		
		Page page=new Page();
		page.setTotalRecord(10);
		page.setPageCount(getOrderForIndexEvt.getPageCount());
		page.setPageNo(getOrderForIndexEvt.getPageNo());
		page.setObjectList(orderDao.getOrderForIndex(evt));
		
		return page;
	}
	/**
	 * 获取交易数量
	 * @param queryOrderListEvt
	 * @return
	 * @author yang
	 */
	public long queryOrderCount(QueryOrderListEvt queryOrderListEvt){
		com.huoyun.order.dao.evt.QueryOrderListEvt evt=new com.huoyun.order.dao.evt.QueryOrderListEvt();
		evt.setAddTime(queryOrderListEvt.getAddTime());
		evt.setCargosOwnerId(queryOrderListEvt.getCargosOwnerId());
		evt.setCarOwnerId(queryOrderListEvt.getCarOwnerId());
		evt.setStatus(queryOrderListEvt.getStatus());
		return orderDao.queryOrderCount(evt);
	}
	
	/**
	 * 获取交易总额
	 * @author yang
	 */
	@Override
	public String queryOrderMoney(QueryOrderListEvt queryOrderListEvt) {
		com.huoyun.order.dao.evt.QueryOrderListEvt evt=new com.huoyun.order.dao.evt.QueryOrderListEvt();
		evt.setAddTime(queryOrderListEvt.getAddTime());
		evt.setCargosOwnerId(queryOrderListEvt.getCargosOwnerId());
		evt.setCarOwnerId(queryOrderListEvt.getCarOwnerId());
		evt.setStatus(queryOrderListEvt.getStatus());
		return orderDao.queryOrderMoney(evt);
	}
	
	/**
	 * 查询货物关联的订单信息
	 * @param cargosOwnerId 货物拥有者id
	 * @param cargosId 货物id
	 * 
	 * @author 刘晓宁
	 * @return 订单信息列表
	 */
	public List<Order> queryOrders(long cargosOwnerId, long cargosId)
	{
		return orderDao.queryOrders(cargosOwnerId, cargosId);
	}
	
	/**
	 * 删除订单，这里只是将订单的状态标示为：999（标示删除）
	 * @param orderId 订单id
	 * @return 
	 * 
	 * @author 刘晓宁
	 */
	public void deleteOrder(long orderId)
	{
		orderDao.deleteOrder(orderId);
	}

	@Override
	public String getOrderId() 
	{
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyMMddHH");
        Date date = new Date();
        String dateStr = dateFormat.format(date);
        
        long orderCount = orderDao.getCountByDay(dateStr);
          
		return dateStr + fillZero(6, String.valueOf(orderCount));
	}
	

	
	
	/**
     * 补零
     * 
     * @param length 补零后的长度
     * @param source需要补零的长符串
     * @return
     */
    private String fillZero(int length, String source) {
        StringBuilder result = new StringBuilder(source);
        for (int i = result.length(); i < length; i++) {
            result.insert(0, '0');
        }
        return result.toString();
    } 

	public OrderDao getOrderDao() {
		return orderDao;
	}

	public void setOrderDao(OrderDao orderDao) {
		this.orderDao = orderDao;
	}

	public OrderLogDao getOrderLogDao() {
		return orderLogDao;
	}

	public void setOrderLogDao(OrderLogDao orderLogDao) {
		this.orderLogDao = orderLogDao;
	}

	public CargoInfoDao getCargoInfoDao() {
		return cargoInfoDao;
	}

	public void setCargoInfoDao(CargoInfoDao cargoInfoDao) {
		this.cargoInfoDao = cargoInfoDao;
	}

	public CarPubInfoDao getCarPubInfoDao() {
		return carPubInfoDao;
	}

	public void setCarPubInfoDao(CarPubInfoDao carPubInfoDao) {
		this.carPubInfoDao = carPubInfoDao;
	}

	public BidInfoDao getBidInfoDao() {
		return bidInfoDao;
	}

	public void setBidInfoDao(BidInfoDao bidInfoDao) {
		this.bidInfoDao = bidInfoDao;
	}



	

}
